/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 * 
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#ifndef CALIBRATOR_H
#define CALIBRATOR_H

#include <vector>
#include <cv.h>

#include "wMatrix.h"
#include "Calibration.h"

namespace windage
{
	class Calibrator
	{
	private:
		Calibration* calibration;
		int chessboardWidthCount;
		int chessboardHeightCount;
		double chessboardFieldSize;
		std::vector<std::vector<CvPoint2D32f>> chessboardPointList;

	public:
		Calibrator()
		{
			calibration = NULL;

			chessboardWidthCount = 7;
			chessboardHeightCount = 8;
			chessboardFieldSize = 28.0;

			chessboardPointList.clear();
		};
		~Calibrator()
		{
			ResetChessboardPointList();
		};

		inline void AttatchCalibration(Calibration* calibration){this->calibration = calibration;};
		inline void Initialize(int width=7, int height=8, double field=28.0)
		{
			this->chessboardWidthCount = width;
			this->chessboardHeightCount = height;
			this->chessboardFieldSize = field;
		};
		inline int GetChessboardPointCount(){return (chessboardWidthCount-1)*(chessboardHeightCount-1);};
		inline Calibration* GetCalibration(){return this->calibration;};

		inline void AddChessboardPoints(std::vector<CvPoint2D32f> resultPoint){this->chessboardPointList.push_back(resultPoint);};
		void ResetChessboardPointList();

		bool FindChessboardCorner(std::vector<CvPoint2D32f>* resultPoint, IplImage* grayImage);
		bool FindChessboardCorner(std::vector<windage::Vector2>* resultPoint, IplImage* grayImage);
		bool GetChessboard(std::vector<windage::Vector2>* resultPoint);

		void DrawChessboardInfo(std::vector<CvPoint2D32f>* resultPoint, IplImage* colorImage);
		bool SolveCalibration();
		bool UpdateExtrinsic(std::vector<CvPoint2D32f>* resultPoint);
	};
}
#endif
